{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0, 1, 2, 3],\n",
      "        [0, 1, 2, 3],\n",
      "        [0, 1, 2, 3],\n",
      "        [0, 1, 2, 3]])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "a = torch.arange(4).expand(4, 4)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([2., 3., 4., 5.])\n"
     ]
    }
   ],
   "source": [
    "b = torch.Tensor([2,3,4,5])\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2., 3., 4., 5.]])\n"
     ]
    }
   ],
   "source": [
    "c = b.unsqueeze(0)\n",
    "print(c)\n",
    "# unsqueeze将为张量增加一维，0表示新增维度在第一维，1表示新增维度在第二维，-1表示新增维度在最后一维。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([2., 3., 4., 5.])\n"
     ]
    }
   ],
   "source": [
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2.],\n",
      "        [3.],\n",
      "        [4.],\n",
      "        [5.]])\n"
     ]
    }
   ],
   "source": [
    "d = b.unsqueeze(-1)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ True,  True, False, False],\n",
      "        [ True,  True,  True, False],\n",
      "        [ True,  True,  True,  True],\n",
      "        [ True,  True,  True,  True]])\n"
     ]
    }
   ],
   "source": [
    "# 测试张量大小比较，返回布尔型\n",
    "f = a < d\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[False],\n",
      "        [False],\n",
      "        [ True],\n",
      "        [ True]])\n"
     ]
    }
   ],
   "source": [
    "e = torch.Tensor([[1], [2], [4], [5]])\n",
    "print(e == d) # 返回的仍然是bool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ True,  True, False, False],\n",
      "        [ True,  True,  True, False],\n",
      "        [ True,  True,  True,  True],\n",
      "        [ True,  True,  True,  True]])\n",
      "tensor(13)\n"
     ]
    }
   ],
   "source": [
    "print(f*f)\n",
    "print(torch.sum(f * f)) # 虽然返回的是bool，但是sum时，true是当做1来加的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mx_py3",
   "language": "python",
   "name": "mx_py3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
